﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}

<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<div class="layui-container" style="padding:20px">
    <div class="layui-row">
        <div class="layui-col-md12">
            <h2>客户详情</h2>
        </div>
    </div>

    <div class="layui-row">
        <div class="layui-col-md8 layui-col-md-offset2">
            <form class="layui-form" lay-filter="clientDetailForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">客户编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="ClientCode" class="layui-input" readonly>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">客户名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="ClientName" class="layui-input" readonly>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">客户类型</label>
                    <div class="layui-input-block">
                        <input type="text" name="CTypeName" class="layui-input" readonly>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-block">
                        <input type="text" name="ClientContact" class="layui-input" readonly>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="text" name="ClientStateText" class="layui-input" readonly>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block">
                        <input type="text" name="ClientTel" class="layui-input" readonly>
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="ClientRemark" class="layui-textarea" rows="3" readonly></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn layui-btn-normal" id="goUpdate"><i class="layui-icon layui-icon-edit"></i> 去修改</button>
                        <button type="button" class="layui-btn layui-btn-primary" id="backToList"><i class="layui-icon layui-icon-left"></i> 返回列表</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 先引入jQuery，再引入其他脚本 -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    // 加载客户类型，构造成 map 以便显示名称
    function loadCTypeMap(callback){
        $.ajax({
            url: 'http://localhost:5054/api/Client/GetCType',
            type: 'GET',
            dataType: 'json',
            success: function(res){
                var map = {};
                if(res && res.code === 200 && res.data){
                    $(res.data).each(function(){
                        if(!this.isDel){
                            map[this.cTypeId] = this.cTypeName;
                        }
                    });
                }
                callback && callback(map);
            },
            error: function(){ callback && callback({}); }
        })
    }

    // 详情
    function loadDetail(clientId){
        loadCTypeMap(function(typeMap){
            $.ajax({
                url: 'http://localhost:5054/api/Client/FanTianClien?ClientId=' + clientId,
                type: 'GET',
                dataType: 'json',
                success: function(res){
                    if(res && (res.code === 200 || res.Code === 200) && res.data){
                        var d = res.data;
                        if (Array.isArray(d) && d.length > 0) d = d[0];

                        var ctypeId = d.cTypeId || d.CTypeId;
                        var ctypeName = d.cTypeName || typeMap[ctypeId] || (ctypeId ? ('类型ID: ' + ctypeId) : '');
                        var state = (d.clientState === true || d.clientState === 'true' || d.clientState === 1 || d.clientState === '1') ? '启用' : '禁用';

                        $('input[name="ClientCode"]').val(d.clientCode || d.ClientCode || '');
                        $('input[name="ClientName"]').val(d.clientName || d.ClientName || '');
                        $('input[name="CTypeName"]').val(ctypeName);
                        $('input[name="ClientContact"]').val(d.clientContact || d.ClientContact || '');
                        $('input[name="ClientStateText"]').val(state);
                        $('input[name="ClientTel"]').val(d.clientTel || d.ClientTel || '');
                        $('textarea[name="ClientRemark"]').val(d.clientRemark || d.ClientRemark || '');
                    } else {
                        layer.msg('加载客户详情失败');
                    }
                },
                error: function(){
                    layer.msg('网络错误，无法加载客户详情');
                }
            })
        })
    }

    $(function(){
        // 接收 id：优先 localStorage（与列表页跳转一致）
        var clientId = localStorage.getItem('clientId');
        if(!clientId){
            // 退化处理：尝试从路径读取最后一段数字
            var segs = location.pathname.split('/');
            var last = segs[segs.length-1];
            if (/^\d+$/.test(last)) clientId = last;
        }
        if(clientId){
            loadDetail(clientId);
        } else {
            layer.msg('未找到客户ID');
        }

        // 返回与去修改
        $('#backToList').click(function(){
            window.location.href = '/Client/Show';
        });
        $('#goUpdate').click(function(){
            if (clientId){
                localStorage.setItem('clientId', clientId);
                window.location.href = '/Client/Update/' + clientId;
            }
        });
    })
</script>